//
//  AppDelegate.swift
//  WB25
//
//  Created by HM on 2017/6/28.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        // 实例化
        window = UIWindow(frame: UIScreen.main.bounds)
        // 背景颜色
        window?.backgroundColor = UIColor.white
        // 设置根控制器
        setupRootViewController()
        window?.makeKeyAndVisible()
        print(HMOAuthViewModel.share.isLogin)
        // 注册通知 更改根控制器
        NotificationCenter.default.addObserver(self, selector: #selector(switchRootViewController), name: HMSWITCHROOTVIEWCONTROLLERNOTI, object: nil)
//        HMSQLManager.share
        return true
    }
    // 监听通知切换根控制器
    @objc private func switchRootViewController(noti: Notification){
        // 判断object 不为nil -> HMMainViewController
        if let _ = noti.object {
            window?.rootViewController = HMMainViewController()
        }else{
            // 判断object 为nil -> HMWelcomeViewController
            window?.rootViewController = HMWelcomeViewController()
        }
    }
   
    // 通过用户是否登录来设置根控制器
    private func setupRootViewController(){
        // 如果登录了 -> WelcomeVc
        if HMOAuthViewModel.share.isLogin {
            window?.rootViewController = HMWelcomeViewController()
        }else{
            // 如果未登录 -> MainVC(访客视图)
            window?.rootViewController = HMMainViewController()
        }
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        print("进来")
        HMHomeDAL.deleteCache()
    }
}

